using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;
using Utilities.Security;

public partial class Admin_Users_PrivatePanel : System.Web.UI.UserControl
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        Alert1.HideMessage();
//        Page.Title = MultipleLanguage.OWNSITE + "-" + MultipleLanguage.USER_PRIVATE.ToLower();
        if (!IsPostBack)
        {
            BindDataToDropdown();
            ValidateControl();
            BindDataWhenEdit();
        }
    }

    private void BindDataToDropdown()
    {
        UserRoles objRole = new UserRoles();
        DataTable dtbRole = new DataTable();
        dtbRole = objRole.Search();
        ddlRole.DataSource = dtbRole;
        ddlRole.DataValueField = "UserRoleID";
        ddlRole.DataTextField = "Description";
        ddlRole.DataBind();
    }

    private void ValidateControl()
    {
        int nLang = Support.GetDefaultLanguage();
        nLang = nLang - 1;
        rfvPassword.ErrorMessage = MultipleLanguage.PASSWORD_EMPTY[nLang ];
        revPassword.ErrorMessage = MultipleLanguage.PASSWORD_SPACE_REMOVE[nLang];
        cpvPassword.ErrorMessage = MultipleLanguage.PASSWORD_NOT_SAME_CONFIRM_PASSWORD[nLang];
    }

    private void BindDataWhenEdit()
    {
        string sEmail =  ConvertData.ConvertToString( Session[Config.SESSION_USEREMAIL]);
        if (sEmail != "" || sEmail != string.Empty)
        {
            Users objUser = new Users();
            DataTable dtb =  objUser.GetUserByEmail(sEmail, 1);
            txtEmail.Text = sEmail;
            txtUserName.Text = ConvertData.ConvertToString(dtb.Rows[0]["UserName"]);
            chkActive.Checked = true ;
            Session["LogedID"] = ConvertData.ConvertToString(dtb.Rows[0]["UserID"]);
        }
        else
        {
            Response.Redirect("~/Login.aspx");
        }  
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        Users objUser = new Users();
        objUser.Data.Email = txtEmail.Text.Trim();
        objUser.Data.Username = txtUserName.Text.Trim();
        objUser.Data.RoleID = ConvertData.ConvertToInt(ddlRole.SelectedValue);
        objUser.Data.Status = EnumeType.ACTIVE;

        ClsEncrypt objEnCrypt = new ClsEncrypt();
        objUser.Data.Password = objEnCrypt.Encrypt(txtPassword.Text);

        int nItemID = ConvertData.ConvertToInt(Session["LogedID"]);
        int nResult = objUser.Update(nItemID);
        if (nResult > 0)
        {
            Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage]);
        }
        else
            Alert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLanguage ]);
              
      
    }
}
